package cn.charlotte.pit.menu.enderchests;

import cn.charlotte.pit.api.PointsAPI;
import cn.charlotte.pit.data.PlayerProfile;
import cn.charlotte.pit.data.sub.PlayerEnderChest;
import cn.charlotte.pit.impl.PlayerPointsAPIImpl;
import cn.charlotte.pit.menu.perk.prestige.PrestigePerkBuyMenu;
import cn.charlotte.pit.perk.PerkGroup;
import cn.charlotte.pit.util.item.ItemBuilder;
import cn.charlotte.pit.util.menu.Button;
import cn.charlotte.pit.util.menu.Menu;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Misoryan
 * @Created_In: 2021/1/13 14:57
 */
public class EnderChestsMenu extends Menu {
    private final PlayerProfile profile;

    public EnderChestsMenu(PlayerProfile profile) {
        this.profile = profile;
    }

    @Override
    public String getTitle(Player player) {
        return "末影箱";
    }

    @Override
    public Map<Integer, Button> getButtons(Player player) {
        Map<Integer, Button> button = new HashMap<>();


        int index = 0;
        PlayerEnderChest defaultEnderChest = profile.getDefaultEnderChest();

        boolean isVip = true; //移除了相关依赖，固定为true
        if (isVip && defaultEnderChest.getRows() <= 3) {
            PlayerEnderChest newDefaultEnderChest = new PlayerEnderChest(6);
            newDefaultEnderChest.getInventory().setContents(defaultEnderChest.getInventory().getContents());
            profile.setEnderChests(newDefaultEnderChest, 0);
        }

        PlayerEnderChest[] enderChests = profile.getEnderChests();
        for (PlayerEnderChest playerEnderChest : enderChests) {
            int finalIndex = index;
            index++;
            button.put(9 + finalIndex * 2, new Button() {
                @Override
                public ItemStack getButtonItem(Player player) {
                    ItemStack build;

                    if (finalIndex == 0) {
                        ItemBuilder itemBuilder = new ItemBuilder(Material.ENDER_CHEST)
                                .name("&a末影箱 #" + finalIndex);
                        if (isVip) {
                            itemBuilder
                                    .lore(
                                            "§7大小: §e6§7/§66 §7(§eVIP+3§7)",
                                            "§7存储你的物资!",
                                            "§r",
                                            "§e左键 §7浏览末影箱!"
                                    );
                        } else {
                            itemBuilder
                                    .lore(
                                            "§7大小: §e3§7/§66 §7VIP3及以上+三行",
                                            "§7存储你的物资!",
                                            "§r",
                                            "§e左键 §7浏览末影箱!"
                                    );
                        }
                        build = itemBuilder.build();
                    } else if (playerEnderChest == null) {
                        build = new ItemBuilder(Material.CHEST)
                                .name("&a末影箱 #" + finalIndex + "§7(§c未解锁§7)")
                                .lore(
                                        "§7大小: §e0" + "§7/§66",
                                        "§7存储你的物资!",
                                        "§r",
                                        "§e右键 §c解锁一行 §7(§b1000点券§7)"
                                )
                                .build();
                    } else {
                        int rows = playerEnderChest.getRows();
                        ItemBuilder itemBuilder = new ItemBuilder(Material.ENDER_CHEST)
                                .name("&a末影箱 #" + finalIndex);
                        if (rows == 6) {
                            itemBuilder.lore(
                                    "§7大小: §e" + rows + "§7/§66",
                                    "§7存储你的物资!",
                                    "§r",
                                    "§e左键 §7浏览末影箱!"
                            );
                        } else {
                            itemBuilder.lore(
                                    "§7大小: §e" + rows + "§7/§66",
                                    "§7存储你的物资!",
                                    "§r",
                                    "§e左键 §7浏览末影箱!",
                                    "§e右键 §c解锁一行 §7(§b1000点券§7)"
                            );
                        }
                        build = itemBuilder.build();
                    }
                    return build;
                }

                @Override
                public void clicked(Player player, int slot, ClickType clickType, int hotbarButton, ItemStack currentItem) {
                    boolean leftClick = clickType.isLeftClick();

                    if (leftClick) {
                        PlayerEnderChest eChest = profile.getEnderChests(finalIndex);
                        if (eChest != null) {
                            eChest.openEnderChest(player);
                        }
                        return;
                    }
                    if (finalIndex == 0) {
                        return;
                    }

                    boolean hasPoints = PlayerPointsAPIImpl.INSTANCE.hasPoints(player, 1000);
                    if (!hasPoints) {
                        player.sendMessage("§c点券余额不足!");
                        return;
                    }
                    PlayerEnderChest ec = profile.getEnderChests(finalIndex);
                    if (ec == null) {
                        ec = new PlayerEnderChest(1);
                    } else {
                        int rows = ec.getRows();
                        if (rows == 6) {
                            return;
                        }
                        PlayerEnderChest newEc = new PlayerEnderChest(rows + 1);
                        newEc.getInventory().setContents(ec.getInventory().getContents());
                        ec = newEc;
                    }
                    PlayerPointsAPIImpl.INSTANCE.costPoints(player, 1000, "扩容末影箱");
                    profile.setEnderChests(ec, finalIndex);

                    openMenu(player);
                }
            });
        }


        return button;
    }

    @Override
    public int getSize() {
        return 27;
    }
}
